package hczc.longcai.com.hccrowdfunding.home.second_invest.activities;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.beans.Address;

/**
 * 添加收货地址
 */
public class AddAddressActivity extends BaseActivity implements View.OnClickListener {

    private static final int STATE_DEFAULT = 0;
    private static final int STATE_NOT_DEFAULT = 1;
    private int state = 1;

    private EditText name;
    private EditText phone;
    private EditText address;
    private String area;
    private ImageView aDefault;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);

        initViews();
    }

    private void initViews() {
        findViewById(R.id.head_back).setOnClickListener(this);
        ((TextView) findViewById(R.id.head_title)).setText("添加地址");
        TextView enter = (TextView) findViewById(R.id.head_right_text);
        enter.setText("完成");
        enter.setVisibility(View.VISIBLE);
        enter.setOnClickListener(this);

        area = "黑龙江省哈尔滨市";
        name = ((EditText) findViewById(R.id.add_address_name));
        phone = ((EditText) findViewById(R.id.add_phone_name));
        address = ((EditText) findViewById(R.id.add_xxdz_name));
        aDefault = ((ImageView) findViewById(R.id.add_address_default));
        aDefault.setOnClickListener(this);

    }

    /**
     * 添加地址
     */
    private void addAddress() {
        Address info = null;
        info = addSuccess(info);
        if (info != null) {
            BaseApplication.dbAddressController.addAddress(info);
            setResult(1);
            finish();
        }

    }

    /**
     * 判断添加地址是否符合要求
     *
     * @param info 要添加的地址
     * @return 无论是否符合要求都返回info，若不符合要求返回null
     */
    private Address addSuccess(Address info) {
        String c_area = area;
        boolean c_default = false;
        String c_name = name.getText().toString().trim();
        String c_phone = phone.getText().toString().trim();
        String c_address = address.getText().toString().trim();

        if (!isEmpty(c_name, c_phone, c_address)) {
            return info;
        }

        if (state == STATE_DEFAULT) {
            c_default = true;
        }
        info = new Address(c_name, c_phone, c_area, c_address, c_default);
        return info;
    }

    /**
     * 判断输入框是否为空
     *
     * @param c_name    收货人姓名
     * @param c_phone   收货人电话
     * @param c_address 收货人地址
     * @return 若都不为空返回true，否则返回false
     */
    private boolean isEmpty(String c_name, String c_phone, String c_address) {
        boolean flag = true;
        if ("".equals(c_name)) {
            Toast.makeText(AddAddressActivity.this, "请输入收货人姓名", Toast.LENGTH_SHORT).show();
            setFocus(name);
            flag = false;
            return flag;
        }
        if ("".equals(c_phone)) {
            Toast.makeText(AddAddressActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
            setFocus(phone);
            flag = false;
            return flag;
        }
        if ("".equals(c_address)) {
            Toast.makeText(AddAddressActivity.this, "请输入详细地址", Toast.LENGTH_SHORT).show();
            setFocus(address);
            flag = false;
            return flag;
        }
        return flag;
    }

    /**
     * 设置EditText的焦点
     *
     * @param et 要设置的EditText
     */
    private void setFocus(EditText et) {
        et.setFocusable(true);
        et.setFocusableInTouchMode(true);
        et.requestFocus();
        et.findFocus();
    }

    /**
     * 判断是否设置为默认地址
     */
    private void isDefault() {
        if (state == STATE_NOT_DEFAULT) {
            aDefault.setSelected(true);
            state = STATE_DEFAULT;
        } else {
            aDefault.setSelected(false);
            state = STATE_NOT_DEFAULT;
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.head_back:
                setResult(1);
                finish();
                break;
            case R.id.head_right_text:
                addAddress();
                break;
            case R.id.add_address_default:
                isDefault();
                break;

        }
    }

}
